package com.wdciti.sccp.iot.common.endpoint;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author JINWEN
 */
@Aspect
@Component
@Slf4j
public class UseEndpointAspect {

//    @Pointcut("@annotation(org.easyiot.common.endpoint.UseEndpoint)")
//    public void pointcut() {
//    }

//    @After("@annotation(useEndpoint)")
//    public void doAfter(JoinPoint joinPoint, UseEndpoint useEndpoint) {
////        EndpointConfig ss = (EndpointConfig)joinPoint.getTarget();
////        Class<?> ssd = AopUtils.getTargetClass(EndpointConfig.class);
//        System.out.println(joinPoint);
//    }

    @Around("@annotation(useEndpoint)")
    public Object doAround(ProceedingJoinPoint pjp, UseEndpoint useEndpoint) throws Throwable {
        Object o = pjp.proceed();
        if (o instanceof EndpointConfig) {
            EndpointConfig epc = (EndpointConfig) o;
            String bean = useEndpoint.endpointBean();
            if (StringUtils.hasText(bean)) {
                epc.getConfigMap().put("epBean", bean);
            }
            epc.getConfigMap().put("epClass", useEndpoint.endpointClass());
//               epc.getConfigMap().put("cfgBean",pjp.getSignature().getName());
        }
        return o;
    }
}
